from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from .models import news,classify
from django.urls import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
# Create your views here.

class Show(View):
    def get(self,request):
        data=news.objects.all()
        return render(request,'index.html',{'data':data})

class Add(View):
    def get(self,request):
        data=classify.objects.all()
        return render(request,'add.html',{'data':data})
    def post(self,request):
        if request.method=='POST':
            title=request.POST.get('title')
            matter=request.POST.get('matter')
            fl=request.POST.get('fl')
            news.objects.create(title=title,matter=matter,fl_id=fl)
            return redirect(reverse('news:news_show'))

class Showw(View):
    def get(self,request):
        data=classify.objects.all()
        for i in data:
            i.showw_data=i.news_set.all()
        return render(request,'showw.html',{'data':data})

def login_action(request):
    if request.method=='POST':
        username=request.POST.get('username','')
        password=request.POST.get('password','')
        # if username == "user" and password == '123456':
        #     return redirect(reverse('news:news_show'))
        user=auth.authenticate(username=username,password=password)
        if user is not None:
            auth.login(request,user)
            return redirect(reverse('news:news_show'))
        else:
            return HttpResponse('dlsb')
    return render(request,'in.html')



